package panel.newFunctionPreview
{
	import assists.Lang;
	import assists.ObjectPool;
	import assists.URI;
	import assists.resource.ImageResource;
	import assists.resource.ResourceLoader;
	
	import com.greensock.TweenLite;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.TransformGestureEvent;
	import flash.geom.Point;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	
	import inerface.INewFunctionPreviewPanel;
	
	public class NewFunctionPreview extends Sprite implements INewFunctionPreviewPanel
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader
		private var _lang:Lang;
		private var _uri:URI;
		
		private var pointList:PointGroupList;
		
		private var currentPage:int = 0;
		private var dataList:Array;
		
		private var contentList:Array;
		
		private var _onClose:Function;
		
		public function NewFunctionPreview()
		{
			dataList = [];
			contentList = [];
		}
		
		public function init(stage:Stage, loader:ResourceLoader, lang:Lang,uri:URI = null):void
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			initThis();
		}
		
		public function get content():Sprite
		{
			return this;
		}
		
		public function set onClose(func:Function):void
		{
			_onClose = func;
		}
		
		public function initThis():void
		{
			var back:Shape = new Shape;
			back.graphics.beginFill(0xDDDDFF,0.9);
			back.graphics.drawRect(0,0,_stage.stageWidth,_stage.stageHeight);
			back.graphics.endFill();
			
			this.addChildAt(back,0);
			
			var haha:Shape = new Shape;
			haha.graphics.beginFill(0xFFFFFF,0);
			haha.graphics.drawCircle(0,0,1);
			haha.graphics.endFill();
			this.addChildAt(haha,1);
			
			pointList = new PointGroupList(_stage.stageWidth * .02);
			pointList.x = (_stage.stageWidth - pointList.width) / 2;
			pointList.y = _stage.stageHeight + 50;
			this.addChildAt(pointList,2);
			
			addEvent();
		}
		
		private function addEvent():void
		{
			if(Multitouch.supportedGestures){
				Multitouch.inputMode = MultitouchInputMode.GESTURE;
				this.addEventListener(TransformGestureEvent.GESTURE_SWIPE,swipeHandler);
			}
		}
		
		private function removeEnent():void
		{
			if(this.hasEventListener(TransformGestureEvent.GESTURE_SWIPE)){
				this.addEventListener(TransformGestureEvent.GESTURE_SWIPE,swipeHandler);
			}
		}
		
		protected function swipeHandler(e:TransformGestureEvent):void
		{
			if(e.offsetX < 0){
				nextPage();
			}else{
				formerPage();
			}
		}
		
		private function formerPage():void
		{
			if(currentPage > 0){
				currentPage = currentPage - 1;
				reposition();
			}else{
				return;
			}
		}
		
		private function nextPage():void
		{
			if(currentPage < dataList.length - 1){
				currentPage = currentPage + 1;
				reposition();
			}else{
				if(_onClose is Function){         //到达最后一页关闭此界面
					dataList = [];
					contentList = [];
					removeEnent();
					
					_onClose();
				}
			}
		}
		
		public function changeData(list:Array):void
		{
			if(dataList.length == 0){
				dataList = list;
				currentPage = 0;
				pointList.createPoint(dataList,repo);
				
				function repo():void
				{
					pointList.x = (_stage.stageWidth - pointList.width) / 2;
					pointList.y = _stage.stageHeight * .97;
					
					//					TweenLite.to(pointList,0.5, {x:(_stage.stageWidth - pointList.width) / 2});
					//					TweenLite.to(pointList,0.5,{y:( _stage.stageHeight - 40)});
				}
				
				addPic();
			}
		}
		
		private function addPic():void
		{
			for(var i:int = 0; i < dataList.length;i++){
				var img:ImageResource 
				if(i == 0){
					img = new ImageResource(_loader,dataList[i]["url"],firstOpen);
				}else{
					img = new ImageResource(_loader,dataList[i]["url"]);
				}
				
				//				img.width = _stage.stageWidth;
				//				img.height = _stage.stageHeight;
				this.addChildAt(img,1);
				contentList.push(img);
			}
			
			reposition();
		}
		
		private function firstOpen():void
		{
			if(contentList.length > 0){
				contentList[0].x = 0;
				contentList[0].y = 0;
			}
			reposition();
		}
		
		private function reposition():void
		{	
			for(var i:int = 0;i < contentList.length;i++){
				
				contentList[i].width = _stage.stageWidth;
				contentList[i].height = _stage.stageHeight;
				
				contentList[i].y = (_stage.stageHeight - contentList[i].height) / 2;
				if(i == currentPage){
					TweenLite.to(contentList[i],0.5,{x:(_stage.stageWidth - contentList[i].width) / 2});
				}else if(i < currentPage){
					TweenLite.to(contentList[i],0.5,{x: -contentList[i].width});
				}else{
					TweenLite.to(contentList[i],0.5,{x: _stage.stageWidth});
				}
			}
			
			changePointState();
		}
		
		private function changePointState():void
		{
			pointList.changeData(currentPage);
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
				contentList = [];
				pointList.clear();
				pointList = null;
			}
		}
	}
}